package com.vvlive.caibao.smartcity.ui;

import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.vvlive.caibao.smartcity.adapter.LifeSubmitEatOrderAdapter;
import com.vvlive.caibao.smartcity.base.BaseActivity;
import com.vvlive.caibao.smartcity.bean.LifeOrderMealEatCateBean;
import com.vvlive.caibao.smartcity.bean.MyLocationBean;
import com.vvlive.caibao.smartcity.utils.MyLocationUtil;
import com.vvlive.caibao.smartcity.view.CommonActionBar;

import org.kymjs.kjframe.ui.BindView;

import java.util.ArrayList;
import java.util.List;

import smartcity.vvlive.caibao.com.smartc.R;

/**
 * Created by lenovo on 2015/7/5.
 * 外卖提交订单页
 */
public class LifeSubmitEatOrdersActivity extends BaseActivity implements MyLocationUtil.GetMyLocation, CompoundButton.OnCheckedChangeListener {
    //去结算按钮
    @BindView(id = R.id.eat_cate_querenjiesuan,click = true)
    Button qujiesuan;
    //支付方式
    @BindView(id = R.id.zhifu_zhifubao,click = true)
    ImageView zhifubao;
    @BindView(id = R.id.zhifu_weixin,click = true)
    ImageView weixin;
    //选项
    @BindView(id = R.id.zaixianzhifu)
    CheckBox zaixian_zf;
    @BindView(id = R.id.huodaofukuan)
    CheckBox huodao_fk;
    //用于已选的美食列表
    List<LifeOrderMealEatCateBean> mYiXuanBeanList = new ArrayList<>();
    //送餐地址
    @BindView(id = R.id.dv_adc,click = false)
    TextView dizhi;
    @BindView(id = R.id.submit_eat_orders_list,click = false)
    ListView yixuanListView;
    //
    @BindView(id = R.id.songcandizhi,click = true)
    LinearLayout songcandizhi;

    @Override
    protected int getContentViewRes() {
        return R.layout.life_submit_eat_orders_activity;
    }

    @Override
    public void initWidget() {
        super.initWidget();
        CommonActionBar actionbar = super.getCommonActionBar();
        actionbar.setTitle("提交订单");
        mYiXuanBeanList = (List<LifeOrderMealEatCateBean>) getIntent().getExtras().getSerializable("smtorder");
        yixuanListView = (ListView) findViewById(R.id.submit_eat_orders_list);
        LifeSubmitEatOrderAdapter orderMealListAdapter = new LifeSubmitEatOrderAdapter(getApplicationContext(),mYiXuanBeanList);
        yixuanListView.setAdapter(orderMealListAdapter);
        setListViewHeightBasedOnChildren(yixuanListView);
        //定位
        new MyLocationUtil(getApplicationContext(),this);
        //
        zaixian_zf.setOnCheckedChangeListener(this);
        huodao_fk.setOnCheckedChangeListener(this);
    }

    @Override
    public void widgetClick(View v) {
        super.widgetClick(v);
        switch (v.getId()){
            case R.id.eat_cate_querenjiesuan://确认结算
                toast("确定");
                break;
            case R.id.songcandizhi://地址选择触发
                Intent intent = new Intent(getApplicationContext(),LifeAdressActivity.class);
                //改成回调请求
                startActivity(intent);
                break;
            case R.id.zhifu_zhifubao://支付宝支付
                toast("支付宝支付");
                break;
            case R.id.zhifu_weixin://微信支付
                toast("微信支付");
                break;
        }
    }
    /**
     * 定位信息回馈
     * */
    @Override
    public void MyLocation(MyLocationBean myLocationBean) {
        dizhi.setText(myLocationBean.getFormatAddress());
    }
    public void setListViewHeightBasedOnChildren(ListView listView) {

        ListAdapter listAdapter = listView.getAdapter();

        if (listAdapter == null) {
            return;
        }
        int totalHeight = 0;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();

        params.height = totalHeight
                + (listView.getDividerHeight() * (listAdapter.getCount() - 1))+50;

//        ((ViewGroup.MarginLayoutParams) params).setMargins(10, 10, 10, 10); // 可删除

        listView.setLayoutParams(params);
    }
    /**
     * 线上线下支付方式选择
     * */
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()){
            case R.id.zaixianzhifu:
                if(isChecked){
                    huodao_fk.setChecked(false);
                    zaixian_zf.setChecked(true);
                }
                break;
            case R.id.huodaofukuan:
                if(isChecked){
                    zaixian_zf.setChecked(false);
                    huodao_fk.setChecked(true);
                }
                break;
        }
        if(!huodao_fk.isChecked() && !zaixian_zf.isChecked()){
            buttonView.setChecked(true);
        }
    }
}
